package testnode;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import javax.rmi.ssl.SslRMIClientSocketFactory;
import javax.rmi.ssl.SslRMIServerSocketFactory;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.log4j.Logger;

import utility.ConfigManager;

import com.healthmarketscience.rmiio.RemoteInputStream;
import com.healthmarketscience.rmiio.RemoteInputStreamClient;

public class TestNode implements Serializable, ITestNode {// extends UnicastRemoteObject implements Serializable, ITestNode {

	private static final long serialVersionUID = 4572244666093684182L;

	public TestNode() throws RemoteException, ConfigurationException
	{
		super();
		Logger.getLogger(this.getClass()).debug("TestNode gestartet");
	}

	@Override
	public boolean installFile(String fileName, RemoteInputStream remoteInputStream) {
		Logger logger = Logger.getLogger(this.getClass());
		String installDir = "";
		InputStream fileData = null;
		OutputStream os = null;
		try
		{
			ConfigManager config = ConfigManager.getInstance();
			installDir = config.getString("installDir");
			fileData = RemoteInputStreamClient.wrap(remoteInputStream);
			File installSubDir = new File(installDir + "/test");
			if(!installSubDir.exists() || !installSubDir.isDirectory())
				installSubDir.mkdir();
			os = new FileOutputStream(installDir + "/test/" + fileName,false);
	    	byte[] buffer = new byte[4096];
	        for (int n; (n = fileData.read(buffer)) != -1; )
	            os.write(buffer, 0, n);
	    } catch (Exception e) {
			logger.error("Fehler bei der Installation:", e);
			return false;
		}
		finally
		{
			try {
				if(fileData != null)
					fileData.close();
				if(os != null)
					os.close();
			} catch (IOException e) {
				logger.error("Fehler beim Schließen des IO-Streams:", e);
				return false;
			}
		}

		logger.debug("installed: " + installDir + "/test/" + fileName);

		return true;
	}

	@Override
	public boolean installDir(String fileName) {
		Logger logger = Logger.getLogger(this.getClass());
		try {
			ConfigManager config = ConfigManager.getInstance();
			String dirName = config.getString("installDir")+"/"+fileName;
			File dir       = new File(dirName);

			if (!dir.exists()) dir.mkdir();

			logger.debug("Verzeichnis erstellt: " + dirName);
	    } catch (Exception e) {
			logger.error("Fehler beim Anlegen des Verzeichnisses:", e);
			return false;
		}

		return true;
	}

	@Override
	public boolean execute(String classname, String method, String[] args) throws RemoteException {
		Logger logger = Logger.getLogger(this.getClass());
		String installDir = "";
		try {
			ConfigManager config = ConfigManager.getInstance();
			installDir = config.getString("installDir");
			String command = "java -cp "+installDir+" "+classname + " \""+args[0]+"\"";
			logger.debug("Befehl wird ausgeführt: "+command);
			Runtime.getRuntime().exec(command);
		} catch (Exception e) {
			logger.error("Fehler beim Ausführen!", e);
			e.printStackTrace();
			return false;
		}

		logger.debug("done");

		return true;
	}

	@Override
	public String status() throws RemoteException {
		// TODO Auto-generated method stub
		return null;
	}
}
